import axios from 'axios'

// 创建一个请求对象
const http = axios.create({
  baseURL: 'http://47.112.233.130:8888/',
  validateStatus: function (status) {
    return true
  },
})

// 设置请求拦截器
http.interceptors.request.use(function (config) {
  if (config.url === '/users/login/' || config.url === '/users/registe/') {
    return config
  }
  let token = window.sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = 'Bearer ' + token
  }
  return config
})

// 设置响应拦截器
http.interceptors.response.use(function (config) {
  return config
})

// 封装后端请求接口
export default {
  // 登录接口
  login(params) {
    return http.post('/users/login/', params)
  },
  // 注册接口
  registe(params) {
    return http.post('/users/register/', params)
  },
  // 获取全部项目
  getAllProject(page) {
    return http.get('/projects/', { params: { page: page } })
  },
  // 删除项目
  deletePro(id) {
    return http.delete(`/projects/${id}/`)
  },
  // 添加项目
  addPro(params) {
    return http.post('/projects/', params)
  },
  // 编辑项目
  editPro(id, params) {
    return http.patch(`/projects/${id}/`, params)
  },
  // 获取项目所有环境
  getEnvs(projectId, page) {
    return http.get(`/environments/`, {
      params: { project: projectId, page: page },
    })
  },
  // 创建环境
  createEnv(params) {
    return http.post('/environments/', params)
  },
  editEnv(projectId, params) {
    return http.put(`/environments/${projectId}/`, params)
  },
  deleteEnv(id) {
    return http.delete(`/environments/${id}/`)
  },
  // 获取项目下所有的任务
  getTask(projectId, page) {
    return http.get(`/tasks/`, {
      params: { project: projectId, page: page },
    })
  },
  // 获取项目下所有的模块
  getModules(projectId, page) {
    return http.get(`/modules/`, {
      params: { project: projectId, page: page },
    })
  },
  // 添加模块
  addModules(params) {
    return http.post('/modules/', params)
  },
  // 编辑模块
  editModules(projectId, params) {
    return http.put(`/modules/${projectId}/`, params)
  },
  // 删除模块
  delModules(id) {
    return http.delete(`/modules/${id}/`)
  },
  //添加测试套件
  createSuite(params) {
    return http.post('/test_suits/', params)
  },
  //添加测试用例
  createCases(params) {
    return http.post('/testcases/', params)
  },
  // 获取接口
  getInterFace(projectId, page) {
    return http.get(`/interfaces/`, {
      params: { project: projectId, page: page },
    })
  },

  // 获取用例数据
  getCase(id) {
    return http.get(`/testcases/${id}/`)
  },
  updateCase(id, params) {
    return http.patch(`/testcases/${id}/`, params)
  },
  // 发送请求调试接口
  runCase(params) {
    return http.post('/request/', params)
  },
}
